package it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractUIComponentTag;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

public class HttpUtils
{
	public static String getPageUrl(HttpServletRequest paramHttpServletRequest)
	{
//		String str1 = paramHttpServletRequest.getServletPath();
//		str1 = paramHttpServletRequest.getHeader("referer");
		
		String requestedUri =
			(String) paramHttpServletRequest.getAttribute("javax.servlet.forward.request_uri");
		
		String contextPath = paramHttpServletRequest.getContextPath();

		String requestedUrl = StringUtils.replace(requestedUri, contextPath, "");
		
		String str2 = paramHttpServletRequest.getQueryString();
		
		if((str2 != null) && (str2.length() > 0))
		{
			requestedUrl = requestedUrl + '?' + str2;
		}
		
		return requestedUrl;
	}

	public static String getPageRelativeUrl(HttpServletRequest paramHttpServletRequest)
	{
		String str1 = paramHttpServletRequest.getServletPath();
		int i = str1.lastIndexOf(47);
		
		if(i > 0)
		{
			str1 = str1.substring(i + 1);
		}
		
		String str2 = paramHttpServletRequest.getQueryString();
		if((str2 != null) && (str2.length() > 0))
		{
			str1 = str1 + '?' + str2;
		}
		
		return str1;
	}

	public static String generateSetPropertyURL(Map paramMap, HttpServletRequest paramHttpServletRequest) throws UnsupportedEncodingException
	{
		return generateSetPropertyURL(paramMap, getPageUrl(paramHttpServletRequest), paramHttpServletRequest, null);
	}

	public static String generateSetPropertyURL(Map paramMap, 
			String paramString,
			HttpServletRequest paramHttpServletRequest, 
			AbstractUIComponentTag paramAbstractUIComponentTag) throws UnsupportedEncodingException
	{
		StringBuffer localStringBuffer = new StringBuffer();

		if(paramAbstractUIComponentTag != null)
		{
			localStringBuffer.append(paramAbstractUIComponentTag.getLocator() + ".bind");
		}
		else
		{
			localStringBuffer.append(".bind");
		}

		localStringBuffer.append('?');
		localStringBuffer.append("goto");
		localStringBuffer.append('=');
		localStringBuffer.append(URLEncoder.encode(paramString, paramHttpServletRequest.getCharacterEncoding()));
		localStringBuffer.append('&');
		localStringBuffer.append("referrer");
		localStringBuffer.append('=');
		localStringBuffer.append(URLEncoder.encode(getPageUrl(paramHttpServletRequest), paramHttpServletRequest.getCharacterEncoding()));
		
		if(paramMap != null)
		{
			Iterator localIterator = paramMap.entrySet().iterator();
			while (localIterator.hasNext())
			{
				Map.Entry localEntry = (Map.Entry) localIterator.next();
				localStringBuffer.append('&');
				localStringBuffer.append(URLEncoder.encode((String) localEntry.getKey(), paramHttpServletRequest.getCharacterEncoding()));
				localStringBuffer.append('=');
				localStringBuffer.append(URLEncoder.encode((String) localEntry.getValue(), paramHttpServletRequest.getCharacterEncoding()));
			}
		}
		return localStringBuffer.toString();
	}

	public static String addContextPath(String paramString, HttpServletRequest paramHttpServletRequest)
	{
		if(paramString.startsWith("/"))
			paramString = paramHttpServletRequest.getContextPath() + paramString;
		return paramString;
	}
}
